<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;
use app\common\model\User;
use app\common\model\Bank;

class BusinessApply extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\BusinessApply';

    protected $hasOne = ['user'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'user_code', 'title' => '申请会员', 'templet' => '<div>{{d.user.account}}</div>'])
            ->addRow(['field' => 'type', 'title' => '店铺类型', 'templet' => '<div>{{d.type == 1?"线上":"线下"}}</div>'])
            ->addRow(['field' => 'title', 'title' => '店铺名称', 'width' => 200])
            ->addRow(['field' => 'contacts', 'title' => '联系人'])
            ->addRow(['field' => 'mobile', 'title' => '手机号', 'width' => 110])
            ->addRow(['field' => 'customer_tel', 'title' => '客服电话', 'width' => 110])
            ->addRow(['field' => 'logo', 'title' => '店铺logo', 'templet' => '#logoTpl'])
            ->addRow(['field' => 'license', 'title' => '营业执照', 'templet' => '#licenseTpl'])
            ->addRow(['field' => 'full_address', 'title' => '店铺地址'])
            ->addRow(['field' => 'create_time', 'title' => '申请时间', 'width' => 150])
            ->addRow(['field' => 'status', 'title' => '状态'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#rightBtn', 'title' => '操作', 'width' => 80]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['title' => '审核', 'width' => 1920, 'height' => 1080]])
            ->addTopButtons(['deleteAll'])
            ->setSearch($this->setSearch())
            ->addExtraHtml(['BusinessApply'])
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '会员编号/用户名/手机号', 'input', '', '\app\common\model\User', 'code|account|mobile,id'])
            ->setSearch(['contacts__mobile__customer_tel', '联系人/手机号/客服电话', 'input', '', '', '', 'like'])
            ->setSearch(['title', '店铺名称', 'input', '', '', '', 'like'])
            ->setSearch(['status', '状态', 'select', [1 => '待审核', 2 => '已审核', 3 => '已驳回']]);

    }

    public function add($name = '', $tpl = 'business_apply/add')
    {
        View::assign([
            'bank' => Bank::select()
        ]);
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }

    public function edit($name = '', $id = '', $tpl = 'business_apply/edit', $info = '')
    {
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

    public function selectUser(): Json
    {
        $param = $this->request->param();
        $user = User::whereLike('code|mobile|nickname', '%' . $param['val'] . '%')
            ->field('id,code,mobile,nickname,syn_level')->where('is_del', 0)->select();

        return json(['code' => 0, 'data' => $user]);
    }

    public function addSave(): Json
    {
        try {
            $param = $this->request->post();
            $user = User::find($param['uid']);
            $this->validate($param, 'BusinessApply');

            if(!empty($param['license'])) $param['license'] = implode(',',$param['license']);
            $result = $this->model::add($param, $user);
            return json($result);
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }
}
